// 初始化路由
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login' // index.vue是可以省略的
import Layout from '@/views/layout' // index.vue是可以省略的

import Home from '@/views/home' // index.vue是可以省略的
// import Video from '@/views/video' // index.vue是可以省略的
// import Question from '@/views/question' // index.vue是可以省略的
// import User from '@/views/user' // index.vue是可以省略的
// import Search from '@/views/search' // index.vue是可以省略的
// import Result from '@/views/search/result'
// import Article from '@/views/article'
// import Profile from '@/views/user/profile.vue'
// import Chat from '@/views/user/chat'
// 采用路由守卫之后，出现路由跳转错误
// vuer-router的push方法内部采用是的promise处理，
// 上面的这个异常是因为我们没有处理catch分支。是vue-router自已代码问题。
// 下面是提供的一个解决方案:改写push方法
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// '可以看见要调用的顺序'
// console.log(store)
// console.log('store.state.tokenInfo.token' + store.state.tokenInfo.token)
// 导入组件
// 这个是普通导入的方式
// import Login from '@/views/login'
Vue.use(VueRouter)

const routes = [
  {
    // 登录
    path: '/login',
    name: 'login',
    component: Login
  }, {
    // 登录
    path: '/',
    name: 'layout',
    component: Layout,
    children: [
      { path: '', component: Home, meta: { isKeepAlive: true } }, // 默认显示的子路由
      { path: '/video', component: () => import('../views/video/index.vue') }, // 视频
      { path: '/question', component: () => import('../views/question/index.vue') }, // 问题
      { path: '/user', component: () => import('../views/user/index.vue') } // 用户

    ]
  },
  // 搜索中心
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/search/index.vue')
  },
  // 搜索结果
  {
    path: '/search/result',
    name: 'searchResult',
    component: () => import('../views/search/result.vue')
  },
  // 文章详情
  {
    path: '/article/:id',
    name: 'article',
    component: () => import('../views/article/index.vue')
  },
  // 编辑资料
  {
    path: '/user/profile',
    name: 'userProfile',
    component: () => import('../views/user/profile.vue')
  },
  // 小智
  {
    path: '/user/chat',
    name: 'userChat',
    component: () => import('../views/user/chat.vue')
  }
  // 首页架子
  // {
  //   path: '/',   ====>  zhe
  //   component: Layout,
  //   children: [
  //       // 欢迎页面
  //       { path: '/', component: Welcome },
  //   }

]

const router = new VueRouter({
  routes
})

export default router
